package com.yh.csx.crm.service.logic;

import com.yh.csx.crm.core.enums.PermissionTypeEnum;
import com.yh.csx.crm.core.exception.BusinessException;
import com.yh.csx.crm.dao.CustomerMapper;
import com.yh.csx.crm.dao.ReportCustSalesMapper;
import com.yh.csx.crm.dao.SysSalesPermissionMapper;
import com.yh.csx.crm.po.SysSalesPermission;
import com.yh.csx.crm.qo.SalesQo;
import com.yh.csx.crm.vo.PermissionVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Huang Zhaoping
 * @date 2019/4/16
 */
@Component
public class PermissionLogic {

    @Autowired
    private SysSalesPermissionMapper salesPermissionMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ReportCustSalesMapper custSalesMapper;

    /**
     * 查询用户数据权限
     *
     * @param salesId
     * @return
     */
    public PermissionVo getPermissionBySalesId(Integer salesId) {
        SysSalesPermission sales = salesPermissionMapper.selectByPrimaryKey(salesId);
        return getPermissionInfo(sales);
    }

    /**
     * 查询用户数据权限
     *
     * @param userId
     * @return
     */
    public PermissionVo getPermissionByUserId(Long userId) {
        SysSalesPermission sales = salesPermissionMapper.findByUserId(userId);
        return getPermissionInfo(sales);
    }

    /**
     * 查询用户数据权限
     *
     * @param sales
     * @return
     */
    public PermissionVo getPermissionInfo(SysSalesPermission sales) {
        if (sales == null) throw new BusinessException("用户不存在");
        // 所有权限
        if (sales.getPermissionType() == PermissionTypeEnum.ALL.getCode()) {
            return new PermissionVo(null);
        }
        List<SysSalesPermission> permissions = null;
        if (sales.getPermissionType() == PermissionTypeEnum.SELF_SUB.getCode()) {
            // 自己及下属
            SalesQo qo = new SalesQo();
            qo.setPathPrefix(sales.getPath() + ",");
            permissions = salesPermissionMapper.selectSalesList(qo);
            permissions.add(sales);
        } else if (sales.getPermissionType() == PermissionTypeEnum.SELF_ORG.getCode()) {
            // 本部门
            SalesQo qo = new SalesQo();
            qo.setOrgId(sales.getOrgId());
            permissions = salesPermissionMapper.selectSalesList(qo);
        } else if (sales.getPermissionType() == PermissionTypeEnum.SPEC_ORG.getCode()) {
            // 指定部门
            if (StringUtils.isNotBlank(sales.getRelatedOrgs())) {
                SalesQo qo = new SalesQo();
                qo.setOrgList(Stream.of(sales.getRelatedOrgs().split(",")).map(Integer::valueOf).collect(Collectors.toList()));
                if (CollectionUtils.isNotEmpty(qo.getOrgList())) {
                    permissions = salesPermissionMapper.selectSalesList(qo);
                }
            }
        }
        if (permissions == null) permissions = new ArrayList<>();
        if (permissions.size() == 0) permissions.add(sales);
        return new PermissionVo(permissions);
    }

    /**
     * 根据用户权限查看允许访问的客户编码列表
     *
     * @param vo
     * @return
     */
    public List<String> getPermitCusCodes(PermissionVo vo) {
        if (vo.isPermitAll()) return null;
        Set<String> cusCodes = new HashSet<>();
        // 客户表
        cusCodes.addAll(customerMapper.selectCustomerNumberByUserIds(vo.getUserIds()));
        // 报表客户资料表
        cusCodes.addAll(custSalesMapper.selectCustIdByJobNos(vo.getWorkNumbers()));
        // 为空时设置个默认，避免空列表查询出错
        if (cusCodes.size() == 0) {
            cusCodes.add("-");
        }
        return new ArrayList<>(cusCodes);
    }

    /**
     * 根据用户权限查看允许访问的省区编码列表
     *
     * @param vo
     * @return
     */
    public List<String> getPermitProvinceCodes(PermissionVo vo) {
        if (vo.isPermitAll()) return null;
        Set<String> provinceCodes = new HashSet<>();
        // 客户表
        provinceCodes.addAll(customerMapper.selectProvinceCodeByUserIds(vo.getUserIds()));
        // 报表客户资料表
        provinceCodes.addAll(custSalesMapper.selectProvinceCodeByJobNos(vo.getWorkNumbers()));
        // 为空时设置个默认，避免空列表查询出错
        if (provinceCodes.size() == 0) {
            provinceCodes.add("-");
        }
        return new ArrayList<>(provinceCodes);
    }


}
